{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1ab7c5f",
   "metadata": {},
   "source": [
    "* 2022/10/11/week08\n",
    "* python课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "231d4800",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 0.代码的注释  \n",
    "> 1.用户输入  \n",
    "> 2.格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83b64b24",
   "metadata": {},
   "source": [
    "## 代码的注释\n",
    "> 1.#，一般写在代码的最上边或者每一行代码的最后，用于代码思路的记录"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92bba2e0",
   "metadata": {},
   "source": [
    "> 2.'''注释内容'''，一般是注释代码块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3a0a6b8",
   "metadata": {},
   "source": [
    "## 1.用户输入\n",
    "\n",
    "> 1. input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d8b10988",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:admin\n"
     ]
    }
   ],
   "source": [
    "username = input(\"username:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0cfda5fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "password:123456\n"
     ]
    }
   ],
   "source": [
    "password = input(\"password:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d43b5b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n"
     ]
    }
   ],
   "source": [
    "# 登录系统\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a011661d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "登陆成功欢迎user1登录\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加条件判断\n",
    "# 假设我们的数据库有用户信息;密码：admin，密码，123456\n",
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景一：输入信息正确 登录成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"登陆成功\"+\"欢迎user1\"+\"登录\")\n",
    "# 情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ba3ec69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123452\n",
      "账号不存在或密码不正确\n",
      "您还有2次尝试的机会\n",
      "账号:admin\n",
      "密码:122222\n",
      "账号不存在或密码不正确\n",
      "您还有1次尝试的机会\n",
      "账号:damin\n",
      "密码:aaaa\n",
      "三次输入失败，请一小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代2：增加循环功能\n",
    "# 假设我们的数据库有用户信息;密码：admin，密码，123456\n",
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1): #i 是一个int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    # 情景一：输入信息正确 登录成功\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"登陆成功\"+\"欢迎user1\"+\"登录\")\n",
    "        break # 终止循环\n",
    "    # 情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请一小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd1dcd08",
   "metadata": {},
   "source": [
    "# 2.格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cd247291",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:rose\n",
      "年龄:19\n",
      "工作:IT\n",
      "收入:900000\n",
      "地区:深圳\n",
      "----- INFO OF rose------\n",
      "年龄:19\n",
      "工作:IT\n",
      "收入:900000\n",
      "地区：深圳\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：字符串拼接\n",
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\")\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''----- INFO OF ''' + name +'''------''' + '''\n",
    "年龄:''' + age+'''\n",
    "工作:''' +job+'''\n",
    "收入:'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "608f16ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method raw_input in module ipykernel.kernelbase:\n",
      "\n",
      "raw_input(prompt='') method of ipykernel.ipkernel.IPythonKernel instance\n",
      "    Forward raw_input to frontends\n",
      "    \n",
      "    Raises\n",
      "    ------\n",
      "    StdinNotImplementedError if active frontend doesn't support stdin.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd9902c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：19\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄：\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bc99adc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:rose\n",
      "年龄: 19\n",
      "工作:IT\n",
      "收入:200000000\n",
      "地区:广州\n",
      "----- INFO OF rose------\n",
      "年龄: 19\n",
      "工作: IT\n",
      "收入: 200000000\n",
      "地区： 广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s 依次有序输入\n",
    "name = input(\"姓名:\")\n",
    "age = int(input(\"年龄: \")) #强制将str转换成int\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info =  '''----- INFO OF %s------\n",
    "姓名：%s\n",
    "年龄: %s\n",
    "工作: %s\n",
    "收入: %s\n",
    "地区： %s'''% (name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f2fe8e41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:rose\n",
      "年龄: 19\n",
      "工作:it\n",
      "收入:1000000000000\n",
      "地区:广州\n",
      "----- INFO OF rose------\n",
      "姓名：rose\n",
      "年龄: 19\n",
      "工作: it\n",
      "收入: 1000000000000\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format()格式化输出函数，参数传递，不需要考虑位置\n",
    "name = input(\"姓名:\")\n",
    "age = int(input(\"年龄: \")) #强制将str转换成int\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info =  '''----- INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄: {年龄}\n",
    "工作: {工作}\n",
    "收入: {收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,收入 = salary,地区 = reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbb35ba2",
   "metadata": {},
   "source": [
    "购物车程序项目：\n",
    "\n",
    "要求：\n",
    "> 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "> 2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "> 3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "> 4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "164a3431",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "登陆成功欢迎user1开始购物\n"
     ]
    }
   ],
   "source": [
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1): #i 是一个int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "   \n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"登陆成功\"+\"欢迎user1\"+\"开始购物\")\n",
    "        break # 终止循环\n",
    "   \n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请一小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "101da46f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20000\n",
      "[('手机', 2000), ('笔', 30), ('水杯', 25), ('面包', 15)]\n"
     ]
    }
   ],
   "source": [
    "goodslist = [('手机',2000),('笔',30),('水杯',25),('面包',15)]\n",
    "usersalary = input(\"\")\n",
    "\n",
    "print(goodslist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d4abc62b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123333\n"
     ]
    }
   ],
   "source": [
    "usernum = input(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c336b832",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'<' not supported between instances of 'list' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m goodsprice \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2000\u001b[39m,\u001b[38;5;241m30\u001b[39m,\u001b[38;5;241m25\u001b[39m,\u001b[38;5;241m15\u001b[39m]\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mgoodsprice\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m<\u001b[39;49m\u001b[43m \u001b[49m\u001b[43musersalary\u001b[49m :\n\u001b[0;32m      3\u001b[0m     usersalary \u001b[38;5;241m=\u001b[39m usersalary \u001b[38;5;241m-\u001b[39m goodsprice\n\u001b[0;32m      4\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m商品成功加入购物车\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: '<' not supported between instances of 'list' and 'str'"
     ]
    }
   ],
   "source": [
    "goodsprice = [2000,30,25,15]\n",
    "\n",
    "if goodsprice < usersalary :\n",
    "    usersalary = usersalary - goodsprice\n",
    "    print(\"商品成功加入购物车\")\n",
    "    print(\"余额：%s\")\n",
    "else:\n",
    "    print(\"余额不足\")\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "72a9b220",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入错误\n"
     ]
    }
   ],
   "source": [
    "if usernum == \"q\" :\n",
    "    print(goodslist)\n",
    "    for goods in goodslist:\n",
    "        print(goods)\n",
    "    print(\"还剩余：\")\n",
    "else:\n",
    "    print('输入错误')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c794682",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
